<?
/**
 * VoiceOne Webservices Interface
 *
 * @package voiceone_webservices
 * @subpackage  db_asterisk
 */

//******************************************************************************
//****************              ASTERISK DATABASE              *****************
//******************************************************************************

// ---- putInDB(family:string,key:string,value:string):int ---------------------
function putInDB($family, $key, $value){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database put ".$family." ".$key." ".$value;
  $ret=sendCommand($cmd);
  return 1;
}

// ---- rmFromDB(family:string,key:string):int ---------------------------------
function rmFromDB($family, $key){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database del ".$family." ".$key;
  $ret=sendCommand($cmd);
  return 1;
}

// ---- getFromDB(family:string,key:string):int ---------------------------------
function getFromDB($family, $key){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database get ".$family." ".$key;
  $ret=sendCommand($cmd);
  $retArr=split("\n",$ret);
  $present=-1;
  $value="error";
  if(is_array($retArr)){
    foreach($retArr as $line){
      if(strstr($line, "Value: ")){
        $present=1;
        $value=substr($line, 7);
        break;
      }else if(strstr($line, "entry not found")){
        $present=0;
        $value="";
        break;
      }
    }
  }
  $res=Array( "present" => $present, "value" => $value );
  return $res;
}